﻿using MediatR;
using ProductionManagement.Application.Command.PreProductionCommand;
using ProductionManagement.Domain.Dtos.PreProductionDtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Dto;
using ProductionManagement.Dtos;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;

namespace ProductionManagement.Application.CommandHandler.PreProductionCommandHandler
{
    /// <summary>
    /// 预生产计划列表
    /// </summary>
    public class PreProductionSelectCommandHandler : IRequestHandler<PreProductionSelectCommand, PageDTO<SelectPreProductionDto>>
    {
        private readonly IPreProducsRepository _preProducsRep;
        private readonly IPreProductionRepository _preProductionRep;
        private readonly ISqlSugarClient db;

        public PreProductionSelectCommandHandler(IPreProducsRepository preProducsRep, IPreProductionRepository preProductionRep, ISqlSugarClient db)
        {
            this._preProducsRep = preProducsRep;
            this._preProductionRep = preProductionRep;
            this.db = db;
        }
        public async Task<PageDTO<SelectPreProductionDto>> Handle(PreProductionSelectCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<PreProduction, PreProducs>((preproduction, preproducs) => new JoinQueryInfos(
                    JoinType.Inner, preproduction.PPDocuNumber == preproducs.PPDocuNumber))
                .Where((preproduction, preproducs) => preproducs.IsDel == false)
                .GroupBy((preproduction, preproducs)=>new { preproduction.PreProductionId, preproduction.PPDocuNumber, preproduction.PPName, preproduction.PPType, preproduction.PPNumTotal, preproduction.PPState, preproduction.PPGeneratedNum, preproduction.PPSmark, preproducs.PPsNum, preproduction.AddName, preproduction.AddTime })
                .Select((preproduction, preproducs) => new SelectPreProductionDto
                {
                    PreProductionId = preproduction.PreProductionId,
                    PPDocuNumber = preproduction.PPDocuNumber,
                    PPName = preproduction.PPName,
                    PPType = preproduction.PPType,
                    PPNumTotal = preproduction.PPNumTotal,
                    PPState = preproduction.PPState,
                    PPGeneratedNum = preproduction.PPGeneratedNum,
                    PPSmark = preproduction.PPSmark,
                    PPsNum = preproducs.PPsNum,
                    AddName = preproduction.AddName,
                    AddTime = preproduction.AddTime,
                });
            if (request.PPState > 0)
            {
                list = list.Where(preproduction => preproduction.PPState == request.PPState);
            }
            if (!string.IsNullOrEmpty(request.PPDocuNumber))
            {
                list = list.Where(preproduction => preproduction.PPDocuNumber.Contains(request.PPDocuNumber));
            }
            if (!string.IsNullOrEmpty(request.PPName))
            {
                list = list.Where(preproduction => preproduction.PPName.Contains(request.PPName));
            }
            if (request.PPType > 0)
            {
                list = list.Where(preproduction => preproduction.PPType == request.PPType);
            }
            var totalCount = await list.CountAsync();

            // 应用分页  
            var preproductionlist = await list
                .OrderByDescending(preproduction => preproduction.PreProductionId)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToListAsync();

            // 准备结果  
            var pages = new PageDTO<SelectPreProductionDto>
            {
                Totalcount = totalCount,
                Pagecount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize),
                list = preproductionlist
            };
            return pages;
        }
    }
}
